
package pri.syl.xiaowu.websocket.netty.server.handler.core.factory.execution;


import pri.syl.xiaowu.websocket.core.template.WebsocketChannelTemplate;
import pri.syl.xiaowu.websocket.core.template.WebsocketRouteTemplate;
import pri.syl.xiaowu.websocket.netty.core.factory.constant.ThreadPoolFactoryConstant;
import pri.syl.xiaowu.websocket.netty.core.factory.execution.ThreadPoolFactoryExecution;
import pri.syl.xiaowu.websocket.netty.server.handler.core.factory.WebsocketServerHandlerFactory;
import pri.syl.xiaowu.websocket.netty.server.thread.execution.WebsocketServerBusinessThreadExecution;
import pri.syl.xiaowu.websocket.netty.server.thread.execution.WebsocketServerCloseThreadExecution;
import pri.syl.xiaowu.websocket.netty.server.thread.execution.WebsocketServerTimeoutThreadExecution;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.timeout.IdleState;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author YunLong
 * @Date 2021/7/18 19:05
 * @Description websocket业务执行器
 **/
public class WebsocketServerHandlerFactoryExecution {
    private WebsocketServerHandlerFactoryExecution() {
    }

    public static void business(WebsocketRouteTemplate websocketRouteTemplate, WebsocketChannelTemplate websocketChannelTemplate, WebSocketFrame webSocketFrame) {
        WebsocketServerBusinessThreadExecution businessThreadExecution = new WebsocketServerBusinessThreadExecution(websocketRouteTemplate, websocketChannelTemplate, webSocketFrame.retain());
        ThreadPoolExecutor businessThreadPool = ThreadPoolFactoryConstant.WEBSOCKET_SERVER_BUSINESS_THREAD_POOL;
        ThreadPoolFactoryExecution.statsThread(businessThreadPool, businessThreadExecution);
    }

    public static void timeoutEvent(IdleState state, WebsocketRouteTemplate websocketRouteTemplate, WebsocketChannelTemplate websocketChannelTemplate) {
        if (WebsocketServerHandlerFactory.timeout(state)) {
            timeoutEvent(websocketRouteTemplate, websocketChannelTemplate);
        }
    }

    public static void closeEvent(WebsocketRouteTemplate websocketRouteTemplate, WebsocketChannelTemplate websocketChannelTemplate) {
        WebsocketServerCloseThreadExecution closeThreadExecution = new WebsocketServerCloseThreadExecution(websocketRouteTemplate, websocketChannelTemplate);
        ThreadPoolExecutor businessThreadPool = ThreadPoolFactoryConstant.WEBSOCKET_SERVER_BUSINESS_THREAD_POOL;
        ThreadPoolFactoryExecution.statsThread(businessThreadPool, closeThreadExecution);
    }

    private static void timeoutEvent(WebsocketRouteTemplate websocketRouteTemplate, WebsocketChannelTemplate websocketChannelTemplate) {
        WebsocketServerTimeoutThreadExecution timeoutThreadExecution = new WebsocketServerTimeoutThreadExecution(websocketRouteTemplate, websocketChannelTemplate);
        ThreadPoolExecutor businessThreadPool = ThreadPoolFactoryConstant.WEBSOCKET_SERVER_BUSINESS_THREAD_POOL;
        ThreadPoolFactoryExecution.statsThread(businessThreadPool, timeoutThreadExecution);

    }
}
